package com.biz.primus.ms.tms.service;

import com.biz.primus.base.exception.BizBusinessException;
import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.model.tms.enums.DeliveryPriority;
import com.biz.primus.model.tms.exception.TmsExceptions;
import com.biz.primus.model.tms.vo.DeliveryFeeVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.tms.dao.po.DeliveryFeePo;
import com.biz.primus.ms.tms.dao.repository.DeliveryFeeRepository;
import com.biz.primus.ms.tms.trans.DeliveryFeePoToDeliveryFeeVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * DeliveryFeeService
 * 配送费服务
 *
 * @author JKLiues
 * @date 2017/11/21
 */
@Service
public class DeliveryFeeService extends AbstractBaseService {

    private final DeliveryFeeRepository deliveryFeeRepository;

    @Autowired
    public DeliveryFeeService(DeliveryFeeRepository deliveryFeeRepository) {
        this.deliveryFeeRepository = deliveryFeeRepository;
    }

    /**
     * 添加,修改对应价格
     *
     * @param reqVo reqVo
     */
    public void update(DeliveryFeeVo reqVo) {
        Long id = reqVo.getId();
        Integer deliveryFee = reqVo.getDeliveryFee();
        DeliveryPriority deliveryPriority = reqVo.getDeliveryPriority();
        DeliveryFeePo deliveryFeePo;
        if (id != null) {
            deliveryFeePo = deliveryFeeRepository.findOne(id);
            AssertUtils.notNull(deliveryFeePo, TmsExceptions.DELIVER_FEE_NOT_FOUND);
        } else {
            deliveryFeePo = new DeliveryFeePo();
            deliveryFeePo.setId(idService.getNextId());
        }
        deliveryFeePo.setDeliveryFee(deliveryFee);
        deliveryFeePo.setDeliveryPriority(deliveryPriority);
        deliveryFeeRepository.save(deliveryFeePo);
    }


    /**
     * 删除
     *
     * @param id id
     */
    public void delete(Long id) {
        deliveryFeeRepository.delete(id);
    }

    /**
     * 根据id查询对应配送价格数据
     *
     * @param id id
     * @return DeliveryFeeVo
     */
    public DeliveryFeeVo findPrice(Long id) {
        DeliveryFeePo deliveryFeePo = deliveryFeeRepository.getOne(id);
        if (null != deliveryFeePo) {
            return new DeliveryFeePoToDeliveryFeeVo().apply(deliveryFeePo);
        }
        return new DeliveryFeeVo();
    }

    /**
     * 根据类型查询配送价格对应数据
     *
     * @param deliveryPriority deliveryPriority
     * @return DeliveryFeeVo
     */
    public DeliveryFeeVo getDeliverFee(DeliveryPriority deliveryPriority) {
        List<DeliveryFeePo> deliveryFeePos = deliveryFeeRepository.findByDeliveryPriority(deliveryPriority);
        if (CollectionUtils.isNotEmpty(deliveryFeePos)) {
            DeliveryFeePo deliveryFeePo = deliveryFeePos.get(0);
            return new DeliveryFeePoToDeliveryFeeVo().apply(deliveryFeePo);
        }
        throw new BizBusinessException(TmsExceptions.DELIVER_FEE_NOT_FOUND);
    }
}
